# -*- coding: utf-8 -*-
'''Game main module.

Contains the entry point used by the run_game.py script.

Feel free to put all your game code here, or in other modules in this "gamelib"
package.
'''

import pyglet
from pyglet.gl import *
from pyglet.window import key

import ctypes

import player
import house
import enemy
from constants import *
from objects import World

from model import obj

def draw_box(rect):
    verts = [rect[0],rect[3], #topleft
             rect[1],rect[3], #topright
             rect[1],rect[2],
             rect[0],rect[2],]
    pyglet.graphics.draw(4, GL_QUADS,
        ('v2i', verts) )

def main():
    window = pyglet.window.Window()
    
    grass = pyglet.resource.image("grass.png")
    grass = pyglet.image.TileableTexture.create_for_image(grass)
    street = pyglet.resource.image("street.png")
    street = pyglet.image.TileableTexture.create_for_image(street)
    daman = player.Bengt(window.width/2, window.height/2)
    buildings = pyglet.graphics.Batch()
    hus = house.HouseA(500, 300, batch=buildings)
    enemies = pyglet.graphics.Batch()
    kalle = enemy.OndaKalle(50, 400, enemies)
    World.player=daman
    World.buildings.append(hus)
    World.enemies.append(kalle)
    
    objfile = pyglet.resource.location("tank.obj").path + "/tank.obj"#quickfix
    tank = obj.OBJ(objfile)
    tank.rotation = 0

    fourfv = ctypes.c_float * 4
    glLightfv(GL_LIGHT0, GL_POSITION, fourfv(10, 20, 20, 0))
    glLightfv(GL_LIGHT0, GL_AMBIENT, fourfv(0.2, 0.2, 0.2, 1.0))
    glLightfv(GL_LIGHT0, GL_DIFFUSE, fourfv(0.8, 0.8, 0.8, 1.0))
    glEnable(GL_LIGHT0)
    glEnable(GL_LIGHTING)

    @window.event
    def on_draw():
        window.clear()
        glDisable(GL_DEPTH_TEST)
        glViewport(0, 0, window.width, window.height)
        glMatrixMode(gl.GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, window.width, 0, window.height, -1, 1)
        glMatrixMode(gl.GL_MODELVIEW)
        grass.blit_tiled(0,0,0,window.width, window.height)
        #buildings.draw()
        #enemies.draw()
        #daman.draw()
        glPushMatrix()
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(60., float(window.width)/window.height, 1., 100.)
        glMatrixMode(GL_MODELVIEW)
        glEnable(GL_DEPTH_TEST)
        gluLookAt(0, 10, 10, 0, 0, 0, 0, 5, 0)
        glRotatef(tank.rotation, 0, 1, 0)
        tank.draw()
        glPopMatrix()
        
    @window.event
    def on_key_press(symbol, modifier):
        if symbol == key.LEFT:
            #daman.start_movin_left()
            tank.rotation -= 10
        if symbol == key.RIGHT:
            #daman.start_movin_right()
            tank.rotation += 10
        if symbol == key.UP:
            daman.start_movin_up()
        if symbol == key.DOWN:
            daman.start_movin_down() 

    @window.event
    def on_key_release(symbol, modifier):
        if symbol == key.LEFT:
            daman.stop_movin_left()
        if symbol == key.RIGHT:
            daman.stop_movin_right()
        if symbol == key.UP:
            daman.stop_movin_up()
        if symbol == key.DOWN:
            daman.stop_movin_down() 
    
    def update(dt):
        for building in World.buildings:
            building.update(dt)
        for enemy in World.enemies:
            enemy.update(dt)
        World.player.update(dt)

    pyglet.clock.schedule_interval(update, 1.0/FPS)
    pyglet.app.run()
